//
//  YXJCVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/14.
//

#import "AQJCVC.h"
#import "AQJCCell.h"
#import "AQJCDetailVC.h"
#import "CascadePickerView.h"
#import "MJRefresh/MJRefresh.h"
#import "AQJCModel.h"
#import "BRTextPickerView.h"
#import "JCSBVC.h"
#import "JCSBTableViewController.h"

@interface AQJCVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic) NSInteger  page;
@property (nonatomic,copy) NSString*  clzt;
@property (nonatomic,copy) NSString*  jcjg;
@property (nonatomic,copy) NSString*  deptId;
@property (nonatomic,copy) NSString*  proId;
@property (nonatomic,copy) NSString*  yhDeptId;
@property (nonatomic,strong) CascadePickerView *pickerView;
@property (nonatomic,strong) UIButton *btn1;
@property (nonatomic,strong) UIButton *btn2;
@property (nonatomic,strong) UIButton *btn3;
/// 单位列表
@property (nonatomic, copy) NSArray * deptList;


@end

@implementation AQJCVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"安全检查管理";
    self.page = 1;
    self.dataArr = [NSMutableArray array];
    self.clzt = @"";
    self.deptId = @"";
    self.proId = @"";
    self.jcjg = @"";
    self.yhDeptId = @"";
    
    [self initUI];
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetDeptRel parameters:@{} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            weakSelf.deptList = data[@"data"];
            [WDGlobal shareInstance].deptList = weakSelf.deptList;
        }
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];

    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(add)];
    self.navigationItem.rightBarButtonItem = right;
    
    
}
- (void)add {
    UIStoryboard *userStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    JCSBTableViewController *vc = [userStoryboard instantiateViewControllerWithIdentifier:@"JCSBTableViewController"];
    vc.deptList = self.deptList;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)initUI {
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.offset(50);
    }];
    
    
    UIButton *btn1 = [UIButton buttonWithTitle:@"养护单位" titleColor:main_light_text_color_6 font:[UIFont systemFontOfSize:14] imageName:@"select_icon"  target:self action:@selector(onType:)];
    btn1.tag = 10;
    [topView addSubview:btn1];
    self.btn1 = btn1;
    UIButton *btn2 = [UIButton buttonWithTitle:@"检查结果" titleColor:main_light_text_color_6 font:[UIFont systemFontOfSize:14] imageName:@"select_icon"  target:self action:@selector(onType:)];
    btn2.tag = 11;
    [topView addSubview:btn2];
    self.btn2 = btn2;
    
    UIButton *btn3 = [UIButton buttonWithTitle:@"处理情况" titleColor:main_light_text_color_6 font:[UIFont systemFontOfSize:14] imageName:@"select_icon"  target:self action:@selector(onType:)];
    btn3.tag = 12;
    [topView addSubview:btn3];
    self.btn3 = btn3;
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.offset(0);
        
    }];
    
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        
    }];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.equalTo(btn1.mas_right);
        make.right.equalTo(btn3.mas_left);
        make.width.equalTo(btn1.mas_width);
        make.width.equalTo(btn3.mas_width);

    }];
    
    [btn1 layoutButtonWithEdgeInsetsStyle:TYButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [btn2 layoutButtonWithEdgeInsetsStyle:TYButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [btn3 layoutButtonWithEdgeInsetsStyle:TYButtonEdgeInsetsStyleRight imageTitleSpace:5];
    
    
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.offset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    kWEAK_SELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf initRequest];
    }];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf initRequest];
    }];
    [footer setTitle:@"没有更多了" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
    [self.tableView.mj_header beginRefreshing];

}
-(void)initRequest{
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetSecurityCheck parameters:@{
        @"clzt":self.clzt,
        @"deptId":self.deptId,
        @"jcjg":self.jcjg,
        @"proId":self.proId,
        @"yhDeptId":self.yhDeptId,
        @"pageNum": @(weakSelf.page),
        @"pageSize": @20
      } successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        // 进入DispatchGroup，表示开始一个任务
        NSLog(@"-------1111-----------");
        if(success) {
            if(self.page == 1){
                [weakSelf.dataArr removeAllObjects];
            }
            
           [weakSelf.dataArr addObjectsFromArray:[NSArray yy_modelArrayWithClass:AQJCModel.class json:data[@"data"][@"list"]]];
            [weakSelf.tableView reloadData];
            
            self.hasNextPage =  [data[@"data"][@"hasNextPage"] boolValue];
     
            
        }
        if(weakSelf.hasNextPage){
            [weakSelf.tableView.mj_footer endRefreshing];
        }else{
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
        
    } failureBlock:^(NSString * _Nonnull description) {
        // 进入DispatchGroup，表示开始一个任务
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
- (void)onType:(UIButton *)btn {
    NSInteger tag = btn.tag-10;
    [self showPickerView:tag];
    NSLog(@"%ld", (long)btn.tag);
}

- (void)showPickerView:(NSInteger)tag {
    // 准备左侧数据
    NSArray *leftData = @[@"处理状态", @"待整改", @"已派发",@"处理中",@"已完成",@"已关闭"];
    // 创建并显示 picker

    if(tag == 1){
        leftData = @[@"检查结果",@"正常",@"异常",@"异常且严重"];
        self.pickerView = [[CascadePickerView alloc] initWithLeftData:leftData];

    }else if (tag == 0){
        NSMutableArray * arr = @[].mutableCopy;
        [arr addObject:@"养护单位"];
        for (NSDictionary * dict in self.deptList) {
            [arr addObject:dict[@"deptName"]];
        }
        leftData = arr.copy;
    }
    
    
    self.pickerView = [[CascadePickerView alloc] initWithLeftData:leftData columns:1];
    
    
    // 设置代理
//    pickerView.delegate = self;
    
    // 或者使用闭包
    kWEAK_SELF;
    weakSelf.pickerView.completionIndexBlock = ^(NSInteger leftIndex, NSInteger rightIndex) {
        if(tag == 1){
            weakSelf.jcjg = [NSString stringWithFormat:@"%ld",leftIndex - 1] ;
            [weakSelf.btn2 setTitle:leftData[leftIndex] forState:UIControlStateNormal];
            if([leftData[leftIndex] isEqualToString: @"检查结果"]){
                weakSelf.jcjg = @"";
            }
            [weakSelf.tableView.mj_header beginRefreshing];

        }
        if(tag == 2){
            [weakSelf.btn3 setTitle:leftData[leftIndex] forState:UIControlStateNormal];
            NSString * leftValue = leftData[leftIndex];
            if([leftValue isEqualToString: @"处理状态"]){
                weakSelf.clzt = @"";
            }else{
                weakSelf.clzt = [NSString stringWithFormat:@"%ld",leftIndex] ;

            }
            if([weakSelf.clzt isEqualToString:@"5"]){
                weakSelf.clzt = @"9";
            }
            [weakSelf.tableView.mj_header beginRefreshing];
            
        }
        if(tag == 0){
            [weakSelf.btn1 setTitle:leftData[leftIndex] forState:UIControlStateNormal];
            if([leftData[leftIndex] isEqualToString: @"养护单位"]){
                weakSelf.yhDeptId = @"";
            }else{
                NSDictionary * dict = weakSelf.deptList[leftIndex-1];
                weakSelf.yhDeptId = dict[@"bh"];
            }
            [weakSelf.tableView.mj_header beginRefreshing];

            
        }
        
    };
    
    [self.pickerView show];
}
#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 190;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AQJCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[AQJCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (indexPath.row < self.dataArr.count) {
        cell.model =  self.dataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AQJCDetailVC *vc = [[AQJCDetailVC alloc] init];
    AQJCModel * model = self.dataArr[indexPath.row];
    vc.ID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =[UIColor clearColor];
                _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
